{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Lonely Pixel I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findLonelyPixel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #孤独像素 I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个大小为 <code>m x n</code> 的图像 <code>picture</code> ，图像由黑白像素组成，<code>'B'</code> 表示黑色像素，<code>'W'</code> 表示白色像素，请你统计并返回图像中 <strong>黑色</strong> 孤独像素的数量。</p>\n",
    "\n",
    "<p><strong>黑色孤独像素</strong> 的定义为：如果黑色像素 <code>'B'</code> 所在的同一行和同一列不存在其他黑色像素，那么这个黑色像素就是黑色孤独像素。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/11/11/pixel1.jpg\" style=\"width: 242px; height: 242px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>picture = [[\"W\",\"W\",\"B\"],[\"W\",\"B\",\"W\"],[\"B\",\"W\",\"W\"]]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>全部三个 'B' 都是黑色的孤独像素\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/11/11/pixel2.jpg\" style=\"width: 242px; height: 242px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>picture = [[\"B\",\"B\",\"B\"],[\"B\",\"B\",\"W\"],[\"B\",\"B\",\"B\"]]\n",
    "<strong>输出：</strong>0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m ==&nbsp;picture.length</code></li>\n",
    "\t<li><code>n ==&nbsp;picture[i].length</code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 500</code></li>\n",
    "\t<li><code>picture[i][j]</code> 为 <code>'W'</code> 或 <code>'B'</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [lonely-pixel-i](https://leetcode.cn/problems/lonely-pixel-i/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [lonely-pixel-i](https://leetcode.cn/problems/lonely-pixel-i/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[\"W\",\"W\",\"B\"],[\"W\",\"B\",\"W\"],[\"B\",\"W\",\"W\"]]', '[[\"B\",\"B\",\"B\"],[\"B\",\"B\",\"W\"],[\"B\",\"B\",\"B\"]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m,n = len(picture),len(picture[0])\n",
    "        row,col = [0]*m,[0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]=='B':\n",
    "                    row[i]+=1\n",
    "                    col[j]+=1\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            if row[i]!=1:continue\n",
    "            for j in range(n):\n",
    "                if col[j]!=1:continue\n",
    "                if picture[i][j]=='B': res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        # #蒙对\n",
    "        # #m行n列\n",
    "        # m = len(picture)\n",
    "        # n = len(picture[0])\n",
    "        # #每一行，每一列B的个数\n",
    "        # row_list = [0]*m\n",
    "        # col_list = [0]*n\n",
    "        # B_count = 0\n",
    "\n",
    "        # #先遍历一遍整个图片获取每行的每一行，每一列B的个数\n",
    "        # for i in range(len(picture)):\n",
    "        #     for j in range(len(picture[0])):\n",
    "        #         if picture[i][j] == \"B\":\n",
    "        #             row_list[i] += 1\n",
    "        #             col_list[j] += 1\n",
    "        #             B_count += 1\n",
    "\n",
    "        # #数所有行中B的个数为1的行数，和所有列中B的个数为1的列数\n",
    "        # row_1 = 0\n",
    "        # col_1 = 0\n",
    "        # row_0 = 0\n",
    "        # col_0 = 0\n",
    "        # for i in row_list:\n",
    "        #     if i == 1:\n",
    "        #         row_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         row_0 += 1\n",
    "\n",
    "        # for i in col_list:\n",
    "        #     if i == 1:\n",
    "        #         col_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         col_0 += 1\n",
    "\n",
    "        # m = m - row_0\n",
    "        # n = n - col_0\n",
    "        # max_count = min(m, n)\n",
    "\n",
    "        # if row_1 != 0 and col_1 != 0:\n",
    "        #     if m == n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1 - (B_count - max_count))\n",
    "        #     elif m > n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1)\n",
    "        #     else:\n",
    "        #         return min(row_1, col_1 - (B_count - max_count))\n",
    "        # else:\n",
    "        #     return 0\n",
    "\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        #---------------------------------------------------------------------------------\n",
    "        #DP暴力解法\n",
    "\n",
    "        #先遍历一遍得到每一行中B的纵坐标\n",
    "        col_list = []\n",
    "        \n",
    "        for i in range(m):\n",
    "            col = []\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    col.append(j)\n",
    "            col_list.append(col)\n",
    "        # print(col_list)\n",
    "        \n",
    "        # dp = [0]*m\n",
    "        # for i in range(len(col_list)):\n",
    "        #     if len(col_list[i]) == 1:\n",
    "        #         mark = True\n",
    "        #         # print(col_list[:i])\n",
    "        #         for k in range(len(col_list[:i])):\n",
    "        #             if col_list[i][0] in col_list[k]:\n",
    "        #                 mark = False\n",
    "        #                 if dp[k] > 0:\n",
    "        #                     dp[k] -= 1\n",
    "        #         if mark:\n",
    "        #             dp[i] += 1\n",
    "        #     else:\n",
    "        #         for j in range(len(col_list[i])):\n",
    "        #             # print(col_list[:i])\n",
    "        #             for k in range(len(col_list[:i])):\n",
    "        #                 if col_list[i][j] in col_list[k]:\n",
    "        #                     if dp[k] > 0:\n",
    "        #                         dp[k] -= 1\n",
    "        # return sum(dp)\n",
    "\n",
    "        #--------------------------------------------------------------------------------\n",
    "        #dp的一点点优化:一旦找到一个孤独B就删掉那一行，也相当于删掉了那一列\n",
    "        dp = 0\n",
    "        #循环次数\n",
    "        iter_num = min(m,n)\n",
    "        # print(iter_num)\n",
    "        for i in range(iter_num):\n",
    "            found = False\n",
    "            for j in range(len(col_list)):\n",
    "                # print(j)\n",
    "                if len(col_list[j]) == 1:\n",
    "                    lone = True\n",
    "                    for k in range(len(col_list)):\n",
    "                        if k != j:\n",
    "                            if col_list[j][0] in col_list[k]:\n",
    "                                lone = False\n",
    "                                break\n",
    "                    # print(col_list)\n",
    "                    if lone:\n",
    "                        dp += 1\n",
    "                        del col_list[j]\n",
    "                        found = True\n",
    "                        break\n",
    "            # print(dp)\n",
    "            if found == False:\n",
    "                break\n",
    "        return dp\n",
    "        #-------------------------------------------------------------------------------------\n",
    "        # #一遍过（错误）\n",
    "        # rol_list = []\n",
    "        # col_list = []\n",
    "        # rol_cnt = [0]*m\n",
    "        # col_cnt = [0]*n\n",
    "\n",
    "        # for i in range(m):\n",
    "        #     for j in range(n):\n",
    "        #         if picture[i][j] == \"B\":\n",
    "        #             if i not in rol_list and j not in col_list:\n",
    "        #                 rol_cnt[i] = 1\n",
    "        #                 col_cnt[j] = 1\n",
    "        #             else:\n",
    "        #                 rol_cnt[i] = 0\n",
    "        #                 col_cnt[j] = 0\n",
    "        #             rol_list.append(i)\n",
    "        #             col_list.append(j)\n",
    "        # #由于遍历的方式，到下一行一行，往前一行就再也不会回去了，所以行不会更新重复的信息，同样，在同一行里面的重复，在列里面也记录不到\n",
    "        # return min(sum(rol_cnt), sum(col_cnt))#错的\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            # print(dp)\n",
    "        # print(4 in col_list[2])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # return min(row_1 - (m - min(m,n)), col_1 - (n - min(m,n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        R = len(picture)\n",
    "        C = len(picture[0])\n",
    "        for row in range(R):\n",
    "            for col in range(C):\n",
    "                if picture[row][col]=='B':\n",
    "                    picture[row][col] = 1\n",
    "                else:\n",
    "                    picture[row][col] = 0\n",
    "        print(picture)\n",
    "        ans = 0\n",
    "        for row in range(R):\n",
    "            for col in range(C):\n",
    "                flag = 0\n",
    "                if picture[row][col] == 1:\n",
    "                    print(row,sum(picture[row][i] for i in range(C)))\n",
    "                    print(col,sum(picture[i][col] for i in range(R)))\n",
    "                    if (sum(picture[i][col] for i in range(R))) == 1 and (sum(picture[row][i] for i in range(C)))==1:\n",
    "                        ans +=1      \n",
    "        return ans\n",
    "\n",
    "\n",
    "        # queue = collections.deque()\n",
    "        # visited = [[False] * C for _ in range(R)]\n",
    "        # ans = 0\n",
    "        # for row in range(R):\n",
    "        #     for col in range(C):\n",
    "        #         if not visited[row][col] == 0 or picture[row][col]=='W':\n",
    "        #             continue\n",
    "        #         flag = 1\n",
    "        #         queue.append([row,col])\n",
    "        #         visited[row][col] = 1\n",
    "        #         while queue:\n",
    "        #             cur_row, cur_col = queue.popleft()\n",
    "        #             for i in range(C):\n",
    "        #                 if picture[cur_row][i]=='B' and visited[cur_row][i]==0:\n",
    "        #                     queue.append([cur_row,i])\n",
    "        #                     visited[cur_row][i]=1\n",
    "        #                     flag = 0\n",
    "        #             for i in range(R):\n",
    "        #                 if picture[i][cur_col]=='B'and visited[i][cur_col]==0:\n",
    "        #                     queue.append([i,cur_col])\n",
    "        #                     visited[i][cur_col]=1\n",
    "        #                     flag=0\n",
    "        #         ans += flag\n",
    "        # return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        n,m=len(picture),len(picture[0])\n",
    "        row=[0 for i in range(n)]\n",
    "        col=[0 for i in range(m)]\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if picture[i][j]=='B':\n",
    "                    row[i]+=1\n",
    "                    col[j]+=1\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            if row[i]==1:\n",
    "                for j in  range(m):\n",
    "                    if picture[i][j]=='B' and col[j]==1:\n",
    "                        ans+=1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row_count=defaultdict(int)\n",
    "        col_count=defaultdict(int)\n",
    "        result=0\n",
    "        m=len(picture)\n",
    "        n=len(picture[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    row_count[i]+=1\n",
    "                    col_count[j]+=1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    if row_count[i]==1 and col_count[j]==1:\n",
    "                        result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        row = [0 for _ in range(m)]\n",
    "        col = [0 for _ in range(n)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "        \n",
    "        count = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    if row[i] == col[j] == 1:\n",
    "                        count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row_count=defaultdict(int)\n",
    "        col_count=defaultdict(int)\n",
    "        result=0\n",
    "        m=len(picture)\n",
    "        n=len(picture[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    row_count[i]+=1\n",
    "                    col_count[j]+=1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    if row_count[i]==1 and col_count[j]==1:\n",
    "                        result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        if not picture:\n",
    "            return\n",
    "        n = len(picture)\n",
    "        m = len(picture[0])\n",
    "        row = [0 for _ in range(n)]\n",
    "        col = [0 for _ in range(m)]\n",
    "\n",
    "        for r in range(n):\n",
    "            for c in range(m):\n",
    "                if picture[r][c] == 'B':\n",
    "                    row[r] += 1\n",
    "                    col[c] += 1\n",
    "        res = 0\n",
    "\n",
    "        for r in range(n):\n",
    "            if row[r] == 1:\n",
    "                for c in range(m):\n",
    "                    if col[c] == 1 and picture[r][c] == 'B':\n",
    "                        res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        n=len(picture)\n",
    "        m=len(picture[0])\n",
    "        count=0       \n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                countb=0\n",
    "                if picture[i][j]=='B':\n",
    "                    for k in range(n):\n",
    "                        if picture[k][j]=='B':\n",
    "                            countb+=1\n",
    "                    for g in range(m):\n",
    "                        if picture[i][g]=='B':\n",
    "                            countb+=1\n",
    "                    if countb==2:\n",
    "                        count+=1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row,col=[collections.Counter(r)['B'] for r in picture],[collections.Counter(c)['B'] for c in zip(*picture)]\n",
    "        return sum((row[r] == 1 and col[c] == 1 and picture[r][c] == 'B') for r in range(len(picture)) for c in range(len(picture[0])))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        row_count = [0] * m  # 记录每一行中黑色像素的数量\n",
    "        col_count = [0] * n  # 记录每一列中黑色像素的数量\n",
    "        \n",
    "        # 统计每一行和每一列中黑色像素的数量\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    row_count[i] += 1\n",
    "                    col_count[j] += 1\n",
    "        \n",
    "        # 查找黑色孤独像素\n",
    "        lonely_black_pixels = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    if row_count[i] == 1 and col_count[j] == 1:\n",
    "                        lonely_black_pixels += 1\n",
    "                        \n",
    "        return lonely_black_pixels\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        rc, cc = collections.defaultdict(int), collections.defaultdict(int)\n",
    "\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    rc[i] += 1\n",
    "                    cc[j] += 1\n",
    "        \n",
    "        ret = 0\n",
    "        for i in range(m):\n",
    "            if rc[i] != 1:\n",
    "                continue\n",
    "            for j in range(n):\n",
    "                if cc[j] != 1:\n",
    "                    continue\n",
    "                if picture[i][j] == 'B':\n",
    "                    ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        \n",
    "        row = [0] * m\n",
    "        col = [0] * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "        ans = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] == col[j] == 1 and picture[i][j] == \"B\":\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row = [0 for _ in range(len(picture))]\n",
    "        col = [0 for _ in range(len(picture[0]))]\n",
    "        black = []\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[i])):\n",
    "                if picture[i][j]=='B':\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "                    if row[i]==1 and col[j]==1:\n",
    "                        black.append((i,j))\n",
    "        ans = 0            \n",
    "        for x,y in black:\n",
    "            if row[x]==1 and col[y]==1:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        # #蒙对\n",
    "        # #m行n列\n",
    "        # m = len(picture)\n",
    "        # n = len(picture[0])\n",
    "        # #每一行，每一列B的个数\n",
    "        # row_list = [0]*m\n",
    "        # col_list = [0]*n\n",
    "        # B_count = 0\n",
    "\n",
    "        # #先遍历一遍整个图片获取每行的每一行，每一列B的个数\n",
    "        # for i in range(len(picture)):\n",
    "        #     for j in range(len(picture[0])):\n",
    "        #         if picture[i][j] == \"B\":\n",
    "        #             row_list[i] += 1\n",
    "        #             col_list[j] += 1\n",
    "        #             B_count += 1\n",
    "\n",
    "        # #数所有行中B的个数为1的行数，和所有列中B的个数为1的列数\n",
    "        # row_1 = 0\n",
    "        # col_1 = 0\n",
    "        # row_0 = 0\n",
    "        # col_0 = 0\n",
    "        # for i in row_list:\n",
    "        #     if i == 1:\n",
    "        #         row_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         row_0 += 1\n",
    "\n",
    "        # for i in col_list:\n",
    "        #     if i == 1:\n",
    "        #         col_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         col_0 += 1\n",
    "\n",
    "        # m = m - row_0\n",
    "        # n = n - col_0\n",
    "        # max_count = min(m, n)\n",
    "\n",
    "        # if row_1 != 0 and col_1 != 0:\n",
    "        #     if m == n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1 - (B_count - max_count))\n",
    "        #     elif m > n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1)\n",
    "        #     else:\n",
    "        #         return min(row_1, col_1 - (B_count - max_count))\n",
    "        # else:\n",
    "        #     return 0\n",
    "\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        #---------------------------------------------------------------------------------\n",
    "        #DP暴力解法\n",
    "\n",
    "        # #先遍历一遍得到每一行中B的纵坐标\n",
    "        # col_list = []\n",
    "        \n",
    "        # for i in range(m):\n",
    "        #     col = []\n",
    "        #     for j in range(n):\n",
    "        #         if picture[i][j] == \"B\":\n",
    "        #             col.append(j)\n",
    "        #     col_list.append(col)\n",
    "        # # print(col_list)\n",
    "        \n",
    "        # dp = [0]*m\n",
    "        # for i in range(len(col_list)):\n",
    "        #     if len(col_list[i]) == 1:\n",
    "        #         mark = True\n",
    "        #         # print(col_list[:i])\n",
    "        #         for k in range(len(col_list[:i])):\n",
    "        #             if col_list[i][0] in col_list[k]:\n",
    "        #                 mark = False\n",
    "        #                 if dp[k] > 0:\n",
    "        #                     dp[k] -= 1\n",
    "        #         if mark:\n",
    "        #             dp[i] += 1\n",
    "        #     else:\n",
    "        #         for j in range(len(col_list[i])):\n",
    "        #             # print(col_list[:i])\n",
    "        #             for k in range(len(col_list[:i])):\n",
    "        #                 if col_list[i][j] in col_list[k]:\n",
    "        #                     if dp[k] > 0:\n",
    "        #                         dp[k] -= 1\n",
    "        # return sum(dp)\n",
    "\n",
    "        #--------------------------------------------------------------------------------\n",
    "        # #dp的一点点优化:一旦找到一个孤独B就删掉那一行，也相当于删掉了那一列\n",
    "        # dp = 0\n",
    "        # #循环次数\n",
    "        # iter_num = min(m,n)\n",
    "        # print(iter_num)\n",
    "        # for i in range(iter_num):\n",
    "        #     found = False\n",
    "        #     for j in range(len(col_list)):\n",
    "        #         # print(j)\n",
    "        #         if len(col_list[j]) == 1:\n",
    "        #             lone = True\n",
    "        #             for k in range(len(col_list)):\n",
    "        #                 if k != j:\n",
    "        #                     if col_list[j][0] in col_list[k]:\n",
    "        #                         lone = False\n",
    "        #                         break\n",
    "        #             # print(col_list)\n",
    "        #             if lone:\n",
    "        #                 dp += 1\n",
    "        #                 del col_list[j]\n",
    "        #                 found = True\n",
    "        #                 break\n",
    "        #     # print(dp)\n",
    "        #     if found == False:\n",
    "        #         break\n",
    "        # return dp\n",
    "        #-------------------------------------------------------------------------------------\n",
    "        #一遍过\n",
    "        rol_list = []\n",
    "        col_list = []\n",
    "        rol_cnt = [0]*m\n",
    "        col_cnt = [0]*n\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    if i not in rol_list and j not in col_list:\n",
    "                        rol_cnt[i] = 1\n",
    "                        col_cnt[j] = 1\n",
    "                    else:\n",
    "                        rol_cnt[i] = 0\n",
    "                        col_cnt[j] = 0\n",
    "                    rol_list.append(i)\n",
    "                    col_list.append(j)\n",
    "        #由于遍历的方式，到下一行一行，往前一行就再也不会回去了，所以行不会更新重复的信息，同样，在同一行里面的重复，在列里面也记录不到\n",
    "        return min(sum(rol_cnt), sum(col_cnt))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            # print(dp)\n",
    "        # print(4 in col_list[2])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # return min(row_1 - (m - min(m,n)), col_1 - (n - min(m,n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        return self.anwser_1(picture)\n",
    "\n",
    "    def anwser_1(self, picture):\n",
    "        result = 0\n",
    "\n",
    "        row_len = len(picture)\n",
    "        col_len = len(picture[0])\n",
    "\n",
    "        for row_index in range(row_len):\n",
    "            for col_index in range(col_len):\n",
    "                if picture[row_index][col_index] == \"B\":\n",
    "                    # 检查行\n",
    "                    tmp_row = tmp_col = 0\n",
    "                    for i in range(col_len):\n",
    "                        if picture[row_index][i] == \"B\":\n",
    "                            tmp_row += 1\n",
    "\n",
    "                    # 检查列\n",
    "                    for j in range(row_len):\n",
    "                        if picture[j][col_index] == \"B\":\n",
    "                            tmp_col += 1\n",
    "\n",
    "                    if tmp_col == 1 and tmp_row == 1:\n",
    "                        result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        #每行和每列只能有一个B\n",
    "\n",
    "        #m行n列\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        #每一行，每一列B的个数\n",
    "        row_list = [0]*m\n",
    "        col_list = [0]*n\n",
    "        B_count = 0\n",
    "\n",
    "        #先遍历一遍整个图片获取每行的每一行，每一列B的个数\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[0])):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    row_list[i] += 1\n",
    "                    col_list[j] += 1\n",
    "                    B_count += 1\n",
    "\n",
    "        #数所有行中B的个数为1的行数，和所有列中B的个数为1的列数\n",
    "        row_1 = 0\n",
    "        col_1 = 0\n",
    "        row_0 = 0\n",
    "        col_0 = 0\n",
    "        for i in row_list:\n",
    "            if i == 1:\n",
    "                row_1 += 1\n",
    "            if i == 0:\n",
    "                row_0 += 1\n",
    "\n",
    "        for i in col_list:\n",
    "            if i == 1:\n",
    "                col_1 += 1\n",
    "            if i == 0:\n",
    "                col_0 += 1\n",
    "\n",
    "        m = m - row_0\n",
    "        n = n - col_0\n",
    "        max_count = min(m, n)\n",
    "\n",
    "        if row_1 != 0 and col_1 != 0:\n",
    "            if m == n:\n",
    "                return min(row_1 - (B_count - max_count), col_1 - (B_count - max_count))\n",
    "            elif m > n:\n",
    "                return min(row_1 - (B_count - max_count), col_1)\n",
    "            else:\n",
    "                return min(row_1, col_1 - (B_count - max_count))\n",
    "        else:\n",
    "            return 0\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # return min(row_1 - (m - min(m,n)), col_1 - (n - min(m,n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row = [0 for _ in range(len(picture))]\n",
    "        col = [0 for _ in range(len(picture[0]))]\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[0])):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "        counter = 0\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[0])):\n",
    "                if picture[i][j] == \"B\" and row[i] == 1 and col[j] == 1:\n",
    "                    counter += 1\n",
    "        return counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row_count=defaultdict(int)\n",
    "        col_count=defaultdict(int)\n",
    "        result=0\n",
    "        m=len(picture)\n",
    "        n=len(picture[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    row_count[i]+=1\n",
    "                    col_count[j]+=1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    if row_count[i]==1 and col_count[j]==1:\n",
    "                        result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row, clm = [], []\n",
    "        n ,m = len(picture), len(picture[0])\n",
    "\n",
    "        for i in range(n):\n",
    "            t = 0\n",
    "            for j in range(m):\n",
    "                t += int(picture[i][j] == \"B\")\n",
    "            row.append(t)\n",
    "        \n",
    "        for i in range(m):\n",
    "            t = 0\n",
    "            for j in range(n):\n",
    "                t += int(picture[j][i] == \"B\")\n",
    "            clm.append(t)\n",
    "        \n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                ans += int(picture[i][j] == \"B\" and row[i] == clm[j] == 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[0])):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    flag = True\n",
    "                    for m in range(len(picture[i])):\n",
    "                        if picture[i][m] == \"B\" and m!=j:\n",
    "                            flag = False\n",
    "                            break\n",
    "                    for n in range(len(picture)):\n",
    "                        if picture[n][j] ==\"B\" and n!=i:\n",
    "                            flag = False\n",
    "                            break\n",
    "                    if flag:\n",
    "                        count+=1\n",
    "                else:\n",
    "                    pass\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row_count=defaultdict(int)\n",
    "        col_count=defaultdict(int)\n",
    "        result=0\n",
    "        m=len(picture)\n",
    "        n=len(picture[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    row_count[i]+=1\n",
    "                    col_count[j]+=1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    if row_count[i]==1 and col_count[j]==1:\n",
    "                        result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row=[-1 for _ in range(len(picture))]\n",
    "        line=[-1 for _ in range(len(picture[0]))]\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[0])):\n",
    "                if picture[i][j]=='B':\n",
    "                    if row[i]!=-1:\n",
    "                        row[i]=-2\n",
    "                    if line[j]!=-1:\n",
    "                        line[j]=-2\n",
    "                    if row[i]!=-2:\n",
    "                        row[i]=j\n",
    "                    if line[j]!=-2:\n",
    "                        line[j]=i\n",
    "        ret=0\n",
    "        for i in range(len(row)):\n",
    "            if row[i]!=-2 and row[i]!=-1 and line[row[i]]!=-2 and line[row[i]]!=-1:\n",
    "                ret+=1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        lines = [0] * m\n",
    "        cols = [0] * n\n",
    "        ans = 0\n",
    "        find_col = {}\n",
    "        find_line = {}\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    if lines[i] == 0 and cols[j] == 0:\n",
    "                        lines[i] += 1\n",
    "                        cols[j] += 1\n",
    "                        ans += 1\n",
    "                        find_col[i] = j\n",
    "                        find_line[j] = i\n",
    "                        continue\n",
    "                    \n",
    "                    if lines[i] == 1:\n",
    "                        ans -= 1\n",
    "                        lines[i] = 2\n",
    "                        if cols[find_col[i]] == 1:\n",
    "                            cols[find_col[i]] = 2\n",
    "                    \n",
    "                    if cols[j] == 1:\n",
    "                        ans -= 1\n",
    "                        cols[j] = 2\n",
    "                        if lines[find_line[j]] == 1:\n",
    "                            lines[find_line[j]] = 2\n",
    "                    \n",
    "                    lines[i] = 2\n",
    "                    cols[j] = 2\n",
    "\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "\n",
    "        Rows = [0 for _ in range(m)]\n",
    "        Cols = [0 for _ in range(n)]\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    Rows[i] += 1\n",
    "                    Cols[j] += 1\n",
    "        \n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    if Rows[i] == 1 and Cols[j] == 1:\n",
    "                        res += 1\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        res = 0\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        ml = [0] * m\n",
    "        nl = [0] * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    ml[i] += 1\n",
    "                    nl[j] += 1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\" and ml[i] == 1 and nl[j] == 1:\n",
    "                    res += 1\n",
    "        return res\n",
    "\n",
    "\n",
    "                    \n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        rows, cols = [[] for _ in range(m)], [[] for _ in range(n)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    rows[i].append(j)\n",
    "                    cols[j].append(i)\n",
    "        ans = 0\n",
    "        for i in range(m):\n",
    "            if len(rows[i]) == 1:\n",
    "                tar = rows[i][0]\n",
    "                if len(cols[tar]) == 1:\n",
    "                    ans += 1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        row = [0] * len(picture)\n",
    "        col = [0] * len(picture[0])\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[0])):\n",
    "                if picture[i][j] == 'B':\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "        count = 0\n",
    "        for i in range(len(row)):\n",
    "            for j in range(len(col)):\n",
    "                if picture[i][j] == 'B' and row[i] == 1 and col[j] == 1:\n",
    "                    count += 1\n",
    "        return count\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m,n = len(picture), len(picture[0])\n",
    "        visited_row = [0]*m\n",
    "        visited_col = [0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    visited_row[i] +=1\n",
    "                    visited_col[j] +=1\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if visited_row[i] == 1 and visited_col[j] == 1 and picture[i][j]=='B':\n",
    "                    res +=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        result = 0\n",
    "        row_len, col_len = len(picture), len(picture[0])\n",
    "        tmp_row, tmp_col = [0 for i in range(row_len)], [0 for i in range(col_len)]\n",
    "\n",
    "        for row_index in range(row_len):\n",
    "            for col_index in range(col_len):\n",
    "                if picture[row_index][col_index] != \"B\":\n",
    "                    continue\n",
    "                tmp_row[row_index] += 1\n",
    "                tmp_col[col_index] += 1\n",
    "\n",
    "        for index in range(row_len):\n",
    "            if tmp_row[index] != 1:\n",
    "                continue\n",
    "            for jndex in range(col_len):\n",
    "                if tmp_col[jndex] != 1:\n",
    "                    continue\n",
    "                if picture[index][jndex] != \"B\":\n",
    "                    continue\n",
    "                result += 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        res = 0\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        label_i = [0] * m\n",
    "        label_j = [0] * n\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    label_i[i] += 1\n",
    "                    label_j[j] += 1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B' and label_i[i] == 1 and label_j[j] == 1:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m, n = len(picture), len(picture[0])\n",
    "\n",
    "        row = [0 for i in range(m)]\n",
    "        col = [0 for j in range(n)]\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "            \n",
    "        ret = 0\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B' and row[i] == 1 and col[j] == 1:\n",
    "                    ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        rc, cc = collections.defaultdict(int), collections.defaultdict(int)\n",
    "\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    rc[i] += 1\n",
    "                    cc[j] += 1\n",
    "        \n",
    "        ret = 0\n",
    "        for i in range(m):\n",
    "            if rc[i] != 1:\n",
    "                continue\n",
    "            for j in range(n):\n",
    "                if cc[j] != 1:\n",
    "                    continue\n",
    "                if picture[i][j] == 'B':\n",
    "                    ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        n=len(picture)\n",
    "        m=len(picture[0])\n",
    "        cnt=0\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                count=0\n",
    "                if picture[i][j]=='B':\n",
    "                    for x in range(n):\n",
    "                        if picture[x][j]=='B':\n",
    "                            count+=1\n",
    "                    for y in range(m):\n",
    "                        if picture[i][y]=='B':\n",
    "                            count+=1\n",
    "                    if count==2:\n",
    "                        cnt+=1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        n, m = len(picture), len(picture[0])\n",
    "        row, col = collections.Counter(), collections.Counter()\n",
    "        blacks = []\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "                    blacks.append([i, j])\n",
    "        \n",
    "        result = 0\n",
    "        for r, c in blacks:\n",
    "            if row[r] == 1 and col[c] == 1:\n",
    "                result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        rows, cols = len(picture), len(picture[0])\n",
    "        rowSum = [0 for _ in range(rows)]\n",
    "        colSum = [0 for _ in range(cols)]\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                if picture[i][j] == 'B':\n",
    "                    rowSum[i] += 1\n",
    "                    colSum[j] += 1\n",
    "        \n",
    "        cnt = 0\n",
    "        for i in range(rows):\n",
    "            if rowSum[i] == 1:\n",
    "                for j in range(cols):\n",
    "                    if picture[i][j] == 'B' and colSum[j] == 1:\n",
    "                        cnt += 1\n",
    "        \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        c = 0\n",
    "        for s in picture:\n",
    "            if s.count('B') != 1:\n",
    "                continue\n",
    "            else:\n",
    "                black = s.index('B')\n",
    "                col = [ss[black] for ss in picture]\n",
    "                if col.count('B') == 1:\n",
    "                    c+=1\n",
    "        return c\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel2(self, picture: List[List[str]]) -> int:\n",
    "        row, col = [collections.Counter(r)['B'] for r in picture], [collections.Counter(c)['B'] for c in zip(*picture)]\n",
    "        return sum((row[r] == 1 and col[c] == 1 and picture[r][c] == 'B') for r in range(len(picture)) for c in range(len(picture[0])))\n",
    "        \n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        R, C = len(picture), len(picture[0])\n",
    "        row_sum = [0 for _ in range(R)]\n",
    "        col_sum = [0 for _ in range(C)]\n",
    "        for r in range(R):\n",
    "            for c in range(C):\n",
    "                if picture[r][c] == 'B':\n",
    "                    row_sum[r] += 1\n",
    "                    col_sum[c] += 1\n",
    "        res = 0\n",
    "        for r in range(R):\n",
    "            if row_sum[r] == 1:\n",
    "                for c in range(C):\n",
    "                    if picture[r][c] == 'B' and col_sum[c] == 1:\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(picture)):\n",
    "            for j in range(len(picture[0])):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    flag = True\n",
    "                    for m in range(len(picture[i])):\n",
    "                        if picture[i][m] == \"B\" and m!=j:\n",
    "                            flag = False\n",
    "                            break\n",
    "                    for n in range(len(picture)):\n",
    "                        if picture[n][j] ==\"B\" and n!=i:\n",
    "                            flag = False\n",
    "                            break\n",
    "                    if flag:\n",
    "                        count+=1\n",
    "                else:\n",
    "                    pass\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        temp = zip(*picture)\n",
    "        d = collections.defaultdict(int)\n",
    "        # d_1 = collections.defaultdict(int)\n",
    "        res = 0\n",
    "        picture_ =[\"\".join(item)for item in temp]\n",
    "        del temp\n",
    "        for i, line in enumerate(picture_):\n",
    "            for p in line:\n",
    "                if p =='B':\n",
    "                    d[i]+=1\n",
    "        # del picture_\n",
    "        temp = 0\n",
    "        temp1 = 0\n",
    "        for line in picture:\n",
    "            for i, p in enumerate(line):\n",
    "                if p == 'B' and d[i] == 1:\n",
    "                    temp += 1\n",
    "                if p== 'B':\n",
    "                    temp1 += 1\n",
    "            if temp == 1 and temp1 == 1:\n",
    "                res += 1\n",
    "            temp = 0\n",
    "            temp1 = 0\n",
    "                \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        row = [0]*m\n",
    "        colomun = [0]*n\n",
    "        ans = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    row[i] += 1\n",
    "                    colomun[j] +=1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    if row[i] == colomun[j] == 1:\n",
    "                        ans += 1\n",
    "        return ans \n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        mc = [0]*m\n",
    "        nc = [0]*n\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    mc[i]+=1\n",
    "                    nc[j]+=1\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            if (mc[i] == 1):\n",
    "                for j in range(n):\n",
    "                    if nc[j] == 1 and picture[i][j] == 'B':\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Python3 模拟\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        res = 0\n",
    "        rowlen = len(picture)\n",
    "        visited = set()\n",
    "        for i in range(rowlen):\n",
    "            t = picture[i].count(\"B\")\n",
    "            if t != 1:\n",
    "                continue\n",
    "            colIdx = picture[i].index(\"B\")\n",
    "            if colIdx in visited:\n",
    "                continue\n",
    "            up = i - 1\n",
    "            flag = True\n",
    "            while up >= 0:\n",
    "                if picture[up][colIdx] == \"B\":\n",
    "                    flag = False\n",
    "                    visited.add(colIdx)\n",
    "                    break\n",
    "                up -= 1\n",
    "            down = i + 1\n",
    "            while down < rowlen:\n",
    "                if picture[down][colIdx] == \"B\":\n",
    "                    flag = False\n",
    "                    visited.add(colIdx)\n",
    "                    break\n",
    "                down += 1\n",
    "            if flag:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        b=[]\n",
    "        ans=0\n",
    "        m,n=len(picture),len(picture[0])\n",
    "        row=[0]*m\n",
    "        col=[0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]=='B':\n",
    "                    row[i]+=1\n",
    "                    col[j]+=1\n",
    "                    b.append((i,j))\n",
    "        for x,y in b:\n",
    "            if row[x]==1 and col[y]==1:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        #简单统计行列黑色像素即可把\n",
    "        m,n=len(picture),len(picture[0])\n",
    "        row,col=[0]*m,[0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    row[i]+=1\n",
    "                    col[j]+=1\n",
    "        res=0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\" and row[i]==1 and col[j]==1:\n",
    "                    res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        # #蒙对\n",
    "        # #m行n列\n",
    "        # m = len(picture)\n",
    "        # n = len(picture[0])\n",
    "        # #每一行，每一列B的个数\n",
    "        # row_list = [0]*m\n",
    "        # col_list = [0]*n\n",
    "        # B_count = 0\n",
    "\n",
    "        # #先遍历一遍整个图片获取每行的每一行，每一列B的个数\n",
    "        # for i in range(len(picture)):\n",
    "        #     for j in range(len(picture[0])):\n",
    "        #         if picture[i][j] == \"B\":\n",
    "        #             row_list[i] += 1\n",
    "        #             col_list[j] += 1\n",
    "        #             B_count += 1\n",
    "\n",
    "        # #数所有行中B的个数为1的行数，和所有列中B的个数为1的列数\n",
    "        # row_1 = 0\n",
    "        # col_1 = 0\n",
    "        # row_0 = 0\n",
    "        # col_0 = 0\n",
    "        # for i in row_list:\n",
    "        #     if i == 1:\n",
    "        #         row_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         row_0 += 1\n",
    "\n",
    "        # for i in col_list:\n",
    "        #     if i == 1:\n",
    "        #         col_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         col_0 += 1\n",
    "\n",
    "        # m = m - row_0\n",
    "        # n = n - col_0\n",
    "        # max_count = min(m, n)\n",
    "\n",
    "        # if row_1 != 0 and col_1 != 0:\n",
    "        #     if m == n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1 - (B_count - max_count))\n",
    "        #     elif m > n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1)\n",
    "        #     else:\n",
    "        #         return min(row_1, col_1 - (B_count - max_count))\n",
    "        # else:\n",
    "        #     return 0\n",
    "\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        #DP暴力解法\n",
    "\n",
    "        #先遍历一遍得到每一行中B的纵坐标\n",
    "        col_list = []\n",
    "        \n",
    "        for i in range(m):\n",
    "            col = []\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    col.append(j)\n",
    "            col_list.append(col)\n",
    "        # print(col_list)\n",
    "        \n",
    "        # dp = [0]*m\n",
    "        # for i in range(len(col_list)):\n",
    "        #     if len(col_list[i]) == 1:\n",
    "        #         mark = True\n",
    "        #         # print(col_list[:i])\n",
    "        #         for k in range(len(col_list[:i])):\n",
    "        #             if col_list[i][0] in col_list[k]:\n",
    "        #                 mark = False\n",
    "        #                 if dp[k] > 0:\n",
    "        #                     dp[k] -= 1\n",
    "        #         if mark:\n",
    "        #             dp[i] += 1\n",
    "        #     else:\n",
    "        #         for j in range(len(col_list[i])):\n",
    "        #             # print(col_list[:i])\n",
    "        #             for k in range(len(col_list[:i])):\n",
    "        #                 if col_list[i][j] in col_list[k]:\n",
    "        #                     if dp[k] > 0:\n",
    "        #                         dp[k] -= 1\n",
    "\n",
    "        #dp的一点点优化\n",
    "        dp = 0\n",
    "        #循环次数\n",
    "        iter_num = min(m,n)\n",
    "        print(iter_num)\n",
    "        for i in range(iter_num):\n",
    "            found = False\n",
    "            for j in range(len(col_list)):\n",
    "                # print(j)\n",
    "                if len(col_list[j]) == 1:\n",
    "                    lone = True\n",
    "                    for k in range(len(col_list)):\n",
    "                        if k != j:\n",
    "                            if col_list[j][0] in col_list[k]:\n",
    "                                lone = False\n",
    "                                break\n",
    "                    # print(col_list)\n",
    "                    if lone:\n",
    "                        dp += 1\n",
    "                        del col_list[j]\n",
    "                        found = True\n",
    "                        break\n",
    "            # print(dp)\n",
    "            if found == False:\n",
    "                break\n",
    "        return dp\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            # print(dp)\n",
    "        # print(4 in col_list[2])\n",
    "        # return sum(dp)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # return min(row_1 - (m - min(m,n)), col_1 - (n - min(m,n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        # #蒙对\n",
    "        # #m行n列\n",
    "        # m = len(picture)\n",
    "        # n = len(picture[0])\n",
    "        # #每一行，每一列B的个数\n",
    "        # row_list = [0]*m\n",
    "        # col_list = [0]*n\n",
    "        # B_count = 0\n",
    "\n",
    "        # #先遍历一遍整个图片获取每行的每一行，每一列B的个数\n",
    "        # for i in range(len(picture)):\n",
    "        #     for j in range(len(picture[0])):\n",
    "        #         if picture[i][j] == \"B\":\n",
    "        #             row_list[i] += 1\n",
    "        #             col_list[j] += 1\n",
    "        #             B_count += 1\n",
    "\n",
    "        # #数所有行中B的个数为1的行数，和所有列中B的个数为1的列数\n",
    "        # row_1 = 0\n",
    "        # col_1 = 0\n",
    "        # row_0 = 0\n",
    "        # col_0 = 0\n",
    "        # for i in row_list:\n",
    "        #     if i == 1:\n",
    "        #         row_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         row_0 += 1\n",
    "\n",
    "        # for i in col_list:\n",
    "        #     if i == 1:\n",
    "        #         col_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         col_0 += 1\n",
    "\n",
    "        # m = m - row_0\n",
    "        # n = n - col_0\n",
    "        # max_count = min(m, n)\n",
    "\n",
    "        # if row_1 != 0 and col_1 != 0:\n",
    "        #     if m == n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1 - (B_count - max_count))\n",
    "        #     elif m > n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1)\n",
    "        #     else:\n",
    "        #         return min(row_1, col_1 - (B_count - max_count))\n",
    "        # else:\n",
    "        #     return 0\n",
    "\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        #---------------------------------------------------------------------------------\n",
    "        #DP暴力解法\n",
    "\n",
    "        # #先遍历一遍得到每一行中B的纵坐标\n",
    "        # col_list = []\n",
    "        \n",
    "        # for i in range(m):\n",
    "        #     col = []\n",
    "        #     for j in range(n):\n",
    "        #         if picture[i][j] == \"B\":\n",
    "        #             col.append(j)\n",
    "        #     col_list.append(col)\n",
    "        # # print(col_list)\n",
    "        \n",
    "        # dp = [0]*m\n",
    "        # for i in range(len(col_list)):\n",
    "        #     if len(col_list[i]) == 1:\n",
    "        #         mark = True\n",
    "        #         # print(col_list[:i])\n",
    "        #         for k in range(len(col_list[:i])):\n",
    "        #             if col_list[i][0] in col_list[k]:\n",
    "        #                 mark = False\n",
    "        #                 if dp[k] > 0:\n",
    "        #                     dp[k] -= 1\n",
    "        #         if mark:\n",
    "        #             dp[i] += 1\n",
    "        #     else:\n",
    "        #         for j in range(len(col_list[i])):\n",
    "        #             # print(col_list[:i])\n",
    "        #             for k in range(len(col_list[:i])):\n",
    "        #                 if col_list[i][j] in col_list[k]:\n",
    "        #                     if dp[k] > 0:\n",
    "        #                         dp[k] -= 1\n",
    "        # return sum(dp)\n",
    "\n",
    "        #--------------------------------------------------------------------------------\n",
    "        # #dp的一点点优化:一旦找到一个孤独B就删掉那一行，也相当于删掉了那一列\n",
    "        # dp = 0\n",
    "        # #循环次数\n",
    "        # iter_num = min(m,n)\n",
    "        # print(iter_num)\n",
    "        # for i in range(iter_num):\n",
    "        #     found = False\n",
    "        #     for j in range(len(col_list)):\n",
    "        #         # print(j)\n",
    "        #         if len(col_list[j]) == 1:\n",
    "        #             lone = True\n",
    "        #             for k in range(len(col_list)):\n",
    "        #                 if k != j:\n",
    "        #                     if col_list[j][0] in col_list[k]:\n",
    "        #                         lone = False\n",
    "        #                         break\n",
    "        #             # print(col_list)\n",
    "        #             if lone:\n",
    "        #                 dp += 1\n",
    "        #                 del col_list[j]\n",
    "        #                 found = True\n",
    "        #                 break\n",
    "        #     # print(dp)\n",
    "        #     if found == False:\n",
    "        #         break\n",
    "        # return dp\n",
    "        #-------------------------------------------------------------------------------------\n",
    "        #一遍过\n",
    "        rol_list = []\n",
    "        col_list = []\n",
    "        rol_cnt = [0]*m\n",
    "        col_cnt = [0]*n\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    if i not in rol_list and j not in col_list:\n",
    "                        rol_cnt[i] = 1\n",
    "                        col_cnt[j] = 1\n",
    "                    else:\n",
    "                        rol_cnt[i] = 0\n",
    "                        col_cnt[j] = 0\n",
    "                    rol_list.append(i)\n",
    "                    col_list.append(j)\n",
    "        return min(sum(rol_cnt), sum(col_cnt))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            # print(dp)\n",
    "        # print(4 in col_list[2])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # return min(row_1 - (m - min(m,n)), col_1 - (n - min(m,n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        r, c = len(picture), len(picture[0])\n",
    "        row = [0]*r\n",
    "        col = [0]*c\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                if picture[i][j] == 'B':\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "        count = 0\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                if picture[i][j] == 'B' and row[i] == 1 and col[j] == 1:\n",
    "                    count += 1\n",
    "        print(row, col)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        rc, cc = collections.defaultdict(int), collections.defaultdict(int)\n",
    "\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    rc[i] += 1\n",
    "                    cc[j] += 1\n",
    "        \n",
    "        ret = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == 'B':\n",
    "                    if rc[i] == 1 and cc[j] == 1:\n",
    "                        ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m, n = len(picture), len(picture[0])\n",
    "        row = [0] * m\n",
    "        col = [0] * n\n",
    "        b = []\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "                    b.append((i, j))\n",
    "\n",
    "        return sum(row[i] + col[j] == 2 for i, j in b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        #简单统计行列黑色像素即可把\n",
    "        m,n=len(picture),len(picture[0])\n",
    "        row,col=[0]*m,[0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\":\n",
    "                    row[i]+=1\n",
    "                    col[j]+=1\n",
    "        res=0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if picture[i][j]==\"B\" and row[i]==1 and col[j]==1:\n",
    "                    res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        m,n = len(picture),len(picture[0])\n",
    "        rows = [0]*m\n",
    "        cols = [0]*n\n",
    "        for i,row in enumerate(picture):\n",
    "            for j,x in enumerate(row):\n",
    "                if x == 'B':\n",
    "                    rows[i] +=1\n",
    "                    cols[j] += 1\n",
    "        \n",
    "        return sum(1 for i in range(m) for j in range(n) if picture[i][j] == 'B' and rows[i] == cols[j] == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        n=len(picture)\n",
    "        m=len(picture[0])\n",
    "        prom=[]\n",
    "        pron=[]\n",
    "        for i in range(n):\n",
    "            count=0\n",
    "            for j in range(m):\n",
    "                if picture[i][j]=='B':\n",
    "                    count+=1\n",
    "            if count==1:\n",
    "                prom.append(i)\n",
    "\n",
    "        for j in range(m):\n",
    "            count=0\n",
    "            for i in range(n):\n",
    "                if picture[i][j]=='B':\n",
    "                    count+=1\n",
    "            if count==1:\n",
    "                pron.append(j)\n",
    "        res=0\n",
    "        for i in prom:\n",
    "            for j in pron:\n",
    "                print(i,j,picture[i][j])\n",
    "                if picture[i][j]=='B':\n",
    "                    res+=1\n",
    "        print(pron,prom)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture):\n",
    "        row, col = [0] * len(picture), [0] * len(picture[0])\n",
    "        black = set()\n",
    "        for i, r in enumerate(picture):\n",
    "            for j, c in enumerate(r):\n",
    "                if c == 'B':\n",
    "                    row[i] += 1\n",
    "                    col[j] += 1\n",
    "                    black.add((i, j))\n",
    "        cnt = 0\n",
    "        for i, j in black:\n",
    "            if row[i] == 1 and col[j] == 1:\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        # #蒙对\n",
    "        # #m行n列\n",
    "        # m = len(picture)\n",
    "        # n = len(picture[0])\n",
    "        # #每一行，每一列B的个数\n",
    "        # row_list = [0]*m\n",
    "        # col_list = [0]*n\n",
    "        # B_count = 0\n",
    "\n",
    "        # #先遍历一遍整个图片获取每行的每一行，每一列B的个数\n",
    "        # for i in range(len(picture)):\n",
    "        #     for j in range(len(picture[0])):\n",
    "        #         if picture[i][j] == \"B\":\n",
    "        #             row_list[i] += 1\n",
    "        #             col_list[j] += 1\n",
    "        #             B_count += 1\n",
    "\n",
    "        # #数所有行中B的个数为1的行数，和所有列中B的个数为1的列数\n",
    "        # row_1 = 0\n",
    "        # col_1 = 0\n",
    "        # row_0 = 0\n",
    "        # col_0 = 0\n",
    "        # for i in row_list:\n",
    "        #     if i == 1:\n",
    "        #         row_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         row_0 += 1\n",
    "\n",
    "        # for i in col_list:\n",
    "        #     if i == 1:\n",
    "        #         col_1 += 1\n",
    "        #     if i == 0:\n",
    "        #         col_0 += 1\n",
    "\n",
    "        # m = m - row_0\n",
    "        # n = n - col_0\n",
    "        # max_count = min(m, n)\n",
    "\n",
    "        # if row_1 != 0 and col_1 != 0:\n",
    "        #     if m == n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1 - (B_count - max_count))\n",
    "        #     elif m > n:\n",
    "        #         return min(row_1 - (B_count - max_count), col_1)\n",
    "        #     else:\n",
    "        #         return min(row_1, col_1 - (B_count - max_count))\n",
    "        # else:\n",
    "        #     return 0\n",
    "\n",
    "        m = len(picture)\n",
    "        n = len(picture[0])\n",
    "        #DP暴力解法\n",
    "\n",
    "        #先遍历一遍得到每一行中B的纵坐标\n",
    "        col_list = []\n",
    "        \n",
    "        for i in range(m):\n",
    "            col = []\n",
    "            for j in range(n):\n",
    "                if picture[i][j] == \"B\":\n",
    "                    col.append(j)\n",
    "            col_list.append(col)\n",
    "        # print(col_list)\n",
    "        \n",
    "        # dp = [0]*m\n",
    "        # for i in range(len(col_list)):\n",
    "        #     if len(col_list[i]) == 1:\n",
    "        #         mark = True\n",
    "        #         # print(col_list[:i])\n",
    "        #         for k in range(len(col_list[:i])):\n",
    "        #             if col_list[i][0] in col_list[k]:\n",
    "        #                 mark = False\n",
    "        #                 if dp[k] > 0:\n",
    "        #                     dp[k] -= 1\n",
    "        #         if mark:\n",
    "        #             dp[i] += 1\n",
    "        #     else:\n",
    "        #         for j in range(len(col_list[i])):\n",
    "        #             # print(col_list[:i])\n",
    "        #             for k in range(len(col_list[:i])):\n",
    "        #                 if col_list[i][j] in col_list[k]:\n",
    "        #                     if dp[k] > 0:\n",
    "        #                         dp[k] -= 1\n",
    "\n",
    "        #dp的一点点优化\n",
    "        dp = 0\n",
    "        #循环次数\n",
    "        iter_num = min(m,n)\n",
    "        print(iter_num)\n",
    "        for i in range(iter_num):\n",
    "            found = False\n",
    "            for j in range(len(col_list)):\n",
    "                # print(j)\n",
    "                if len(col_list[j]) == 1:\n",
    "                    lone = True\n",
    "                    for k in range(len(col_list)):\n",
    "                        if k != j:\n",
    "                            if col_list[j][0] in col_list[k]:\n",
    "                                lone = False\n",
    "                    # print(col_list)\n",
    "                    if lone:\n",
    "                        dp += 1\n",
    "                        del col_list[j]\n",
    "                        found = True\n",
    "                        break\n",
    "            # print(dp)\n",
    "            if found == False:\n",
    "                break\n",
    "        return dp\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            # print(dp)\n",
    "        # print(4 in col_list[2])\n",
    "        # return sum(dp)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # return min(row_1 - (m - min(m,n)), col_1 - (n - min(m,n)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findLonelyPixel(self, picture: List[List[str]]) -> int:\n",
    "        arr = []\n",
    "        ans = 0\n",
    "        for row in picture:\n",
    "            cur = []\n",
    "            for ele in row:\n",
    "                if ele == 'W':\n",
    "                    cur.append(0)\n",
    "                else:\n",
    "                    cur.append(1)\n",
    "            arr.append(cur)\n",
    "        \n",
    "        for row in arr:\n",
    "            if sum(row) != 1:\n",
    "                continue\n",
    "            col_id = row.index(1)\n",
    "            col = [row[col_id] for row in arr]\n",
    "            if sum(col) == 1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
